package classroom.edu.info.manager.dao;

import classroom.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private Teacher[] teacher = new Teacher[5];

    public boolean addTeacher(Teacher tea) {

        int index = -1;
        for (int i = 0; i < teacher.length; i++) {
            if (teacher[i] == null) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            teacher[index] = tea;
            return true;
        }
        return false;
    }

    public Teacher[] selectTeacher() {
        return teacher;
    }

    public void deleteTeacher(String id) {
        int index = getIndex(id);
        if(index != -1) {
            teacher[index] = null;
        }
    }

    private int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teacher.length; i++) {
            if (teacher[i].getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(Teacher newTeacher) {
        int index = getIndex(newTeacher.getId());
        teacher[index] = newTeacher;

    }
}

